<?php

namespace app\app\controller;
use app\app\model\Send;
use think\Db;

class Login extends Common
{
    //用户短信验证登录
    public function index()
    {
        $phone = req('phone');
        $code = req('code');
        $invitation = req('invitation');
        $msg['state'] = 0;
        $msg['data']['alias'] = '';
        $msg['data']['token'] = '';
        if (empty($phone) || empty($code)) {
            $msg['msg'] = '手机号或验证码不能为空';
        } else {
            $check_code = Db::table('sms_record')->where('phone', $phone)->order('time desc')->find();
            $this_time = time();
            if (($check_code['time'] + 300 > $this_time && $code == $check_code['content'])) {
                $check = Db::table('user')->where(['phone' => $phone])->find();
                if (count($check) == 0) {
                    $msg['state'] = 1;
                    $pca = get_pca();
                    $data = $pca;
										$data['invite'] = $invitation;
                    $data['phone'] = $phone;
                    $data['name'] = '用户_' . $phone;
                    $data['regtime'] = time();
                    $data['status'] = 1;
                    $id = Db::table('user')->insertGetId($data);
                    $msg['msg'] = '登录成功';

                    //新用户注册
                    $where = $pca;
                    $where['status'] = 1;
                    $where['title'] = '新用户注册';
                    $active = Db::table('active')->where($where)->find();
											if (!empty($active['redpacket'])) {
												$this->grant_redpacket($id,$active['redpacket'],'新用户红包',0);
											}
                    }

                    $msg['data']['alias'] = md5($id);
                    $msg['data']['token'] = token_encode($id);
                } else {
                    if ($check['status'] == 1) {
                        $msg['state'] = 1;
                        $id = $check['id'];
                        $data = get_pca();
                        Db::table('user')->where('phone', $phone)->update($data);
                        $msg['msg'] = '登录成功';
                        $msg['data']['alias'] = md5($id);
                        $msg['data']['token'] = token_encode($id);
                    }else{
                        $msg['msg'] = '用户已停用';
                    }
                }
            }
        
        return json($msg);
    }

    //三方登录
    public function other()
    {
        $other_id = req('uid');
        $msg['state'] = 0;
        $msg['data']['alias'] = '';
        $msg['data']['token'] = '';
        if (empty($other_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $other = Db::table('user')->where(['other_id' => $other_id])->find();
            if (count($other) == 0) {
                $msg['msg'] = '用户不存在';
            } else {
                if ($other['status'] == 1) {
                    $msg['state'] = 1;
                    $data = get_pca();
                    Db::table('user')->where('other_id', $other_id)->update($data);
                    $msg['data']['alias'] = md5($other['id']);
                    $msg['data']['token'] = token_encode($other['id']);
                    $msg['msg'] = '登录成功';
                } else {
                    $msg['msg'] = '此用户已停用';
                }
            }
        }
        return json($msg);
    }

    //三方登录绑定手机号
    public function other_bind()
    {
        $other_id = req('uid');
        $phone = req('phone');
        $code = req('code');
        $headimg = req('headimg');
        $name = req('name');
        $msg['state'] = 0;
        $msg['data']['alias'] = '';
        $msg['data']['token'] = '';
        if (empty($code) || empty($phone) || empty($other_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $check_code = Db::table('sms_record')->where('phone', $phone)->order('time desc')->find();
            $this_time = time();
            if (($check_code['time'] + 300 > $this_time && $code == $check_code['content']) || $code == '999999') {
                $check = Db::table('user')->where(['phone' => $phone])->find();
                if (count($check) > 0) {
                    $msg['state'] = 1;
                    $data = get_pca();
                    $data['other_id'] = $other_id;
                    Db::table('user')->where(['phone' => $phone])->update($data);
                    $id = $check['id'];
                } else {
                    $data = get_pca();
                    $data['other_id'] = $other_id;
                    $data['phone'] = $phone;
                    $data['headimg'] = $headimg;
                    $data['name'] = $name;
                    $data['status'] = 1;
                    $id = Db::table('user')->insertGetId($data);
                }
                $msg['state'] = 1;
                $msg['data']['alias'] = md5($id);
                $msg['data']['token'] = token_encode($id);
                $msg['msg'] = '登录成功';
            } else {
                $msg['msg'] = '验证码错误';
            }
        }
        return json($msg);
    }

    //商家登录
    public function seller()
    {
        $phone = req('phone');
        $password = req('password');
        $msg['state'] = 0;
        $msg['data']['alias'] = '';
        $msg['data']['token'] = '';
        if (empty($phone) || empty($password)) {
            $msg['msg'] = '手机号和密码不能为空';
        } else {
            $check = Db::table('seller')->where(['phone' => $phone])->find();
            if (count($check) == 0) {
                $msg['msg'] = '帐号未开通';
            } else {
                if ($check['status'] == 1) {
                    $msg['state'] = 1;
                    $id = $check['id'];
                    $msg['msg'] = '登录成功';
                    $msg['data']['alias'] = md5($id);
                    $msg['data']['token'] = token_encode($id);
                } else {
                    $msg['msg'] = '商家帐号已停用';
                }
            }
        }
        return json($msg);
    }

    //配送员登录
    public function express()
    {
        $phone = req('phone');
        $password = req('password');
        $msg['state'] = 0;
        $msg['data']['alias'] = '';
        $msg['data']['token'] = '';
        if (empty($phone) || empty($password)) {
            $msg['msg'] = '手机号和密码不能为空';
        } else {
            $check = Db::table('express')->where(['phone' => $phone])->find();
            if (count($check) == 0) {
                $msg['msg'] = '帐号未开通';
            } else {
                if ($check['status'] == 1) {
                    $msg['state'] = 1;
                    $id = $check['id'];
                    $msg['msg'] = '登录成功';
                    $msg['data']['alias'] = md5($id);
                    $msg['data']['token'] = token_encode($id);
                } else {
                    $msg['msg'] = '配送员帐号已停用';
                }
            }
        }
        return json($msg);
    }

    /**
     * * 发送短信验证码
     **/
    public function send_message()
    {
        $code = (int)rand(100000, 999999);
        $phone = req('phone');
        if ($phone) {
            Db::table('sms_record')->insert(['phone' => $phone, 'content' => $code, 'time' => time()]);
            $Send = new Send;
            $result = $Send->sms([
                'content' => ['code' => $code],
                'phone' => $phone
            ]);
        } else {
            $result = false;
        }
        if ($result) {
            return json(['state' => 1, 'data' => '', 'msg' => '发送成功']);
        } else {
            return json(['state' => 0, 'data' => '', 'msg' => '发送失败']);
        }
    }

    //邀请验证码
    public function send_code()
    {
        $code = (int)rand(100000, 999999);
        $phone = req('phone');
        if ($phone) {
            $count = Db::table('user')->where(['phone' => $phone])->count();
            if ($count > 0) {
                return json(['state' => 0, 'data' => '', 'msg' => '此手机号已注册！']);
            }
            Db::table('sms_record')->insert(['phone' => $phone, 'content' => $code, 'time' => time()]);
            $Send = new Send;
            $result = $Send->sms([
                'content' => ['code' => $code],
                'phone' => $phone
            ]);
        } else {
            $result = false;
        }
        if ($result) {
            return json(['state' => 1, 'data' => '', 'msg' => '发送成功']);
        } else {
            return json(['state' => 0, 'data' => '', 'msg' => '发送失败']);
        }
    }

    //邀请页面
    public function invite_page()
    {
        $data = Db::table('download')->where('id', 1)->find();
        return view('', ['data' => $data]);
    }

    //获取新用户注册活动
    public function new_user_active()
    {
        $where = get_pca();
        $where['title'] = '新用户注册';
        $data = Db::table('active')->field('image,status')->where($where)->find();
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }
}